'''
author:        Wang Chenyang <cy-wang21@mails.tsinghua.edu.cn>
date:          2024-11-12
Copyright © Department of Physics, Tsinghua University. All rights reserved

Description:   处理bibtex文件，将bibtex文件中给定字段输出到新的bibtex文件中
Usage:         python bibtex-processing.py
'''

import bibtexparser

# 定义要保留的字段
FIELDS_TO_KEEP = ["ENTRYTYPE", "ID", "title", "abstract", "year", "annote",
                  "journal", "author", "keywords"]

# 读取上传的BibTeX文件
with open('data/Exported Items.bib', 'r', encoding='utf-8') as bibtex_file:
    bib_database = bibtexparser.load(bibtex_file)

# 过滤出所需的字段
filtered_entries = []
for entry in bib_database.entries:
    filtered_entry = {}
    for field in FIELDS_TO_KEEP:
        curr_val = entry.get(field, '')
        if curr_val != '':
            filtered_entry[field] = curr_val
        
    if entry.get("journal", '').lower() == "physical review letters":
        filtered_entry["weight"] = '5'
    elif entry.get("journal", '').lower() == "physical review a":
        filtered_entry["weight"] = '2'
    elif entry.get("journal", '').lower() == "physical review b":
        filtered_entry["weight"] = '2'
    elif "nature" in entry.get("journal", '').lower():
        filtered_entry["weight"] = '3'
    else:
        filtered_entry["weight"] = '1'

    filtered_entries.append(filtered_entry)

# 按照权重排序
filtered_entries.sort(key=lambda x: (x.get('weight', 0), int(x.get('year', 0))), reverse=True)


# 将过滤后的BibTeX数据库写入新的文件
filtered_bib_filename = 'data/filtered_bib_file.bib'

with open(filtered_bib_filename, 'w', encoding='utf-8'):
    # bibtexparser.dump(new_bib, filtered_bib_file)
    pass

for item in filtered_entries:
    new_bib = bibtexparser.bibdatabase.BibDatabase()
    item.pop("weight", None)
    new_bib.entries = [item]
    with open(filtered_bib_filename, 'a', encoding='utf-8') as filtered_bib_file:
        bibtexparser.dump(new_bib, filtered_bib_file)

filtered_bib_filename